#ifndef LEADERBOARD_H
#define	LEADERBOARD_H

#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include "data_struct_bttn.h"

/**
 * @param MAX_PLAYER stands for the maximum number of players' score in the leaderboard
 * @param MAX_CHAR_NAME stands for the maximum char available in input when you insert your name in the leaderboard
 * @param MAX_SCORE stands for the maximum digits available for the score.
 */
const unsigned int MAX_PLAYER = 10;
const unsigned int MAX_CHAR_NAME = 3;
const unsigned int MAX_SCORE = 6;

/**
 * The structure @struct player represents the object of a player and his attribute:
 * @param name is a array of char where is stored the name of the player .
 * @param points is an integer where is stored the score of the player .
 */
struct player{
  char name[MAX_CHAR_NAME];
  int  points;
};

//=======================================
// INPUT/OUTPUT FUNCTION
//=======================================

///This function loads the leaderboard information from a file
/**
 * The function LoadLeaderBoard opens the file classifica.dat and loads the elements
 * from file into the data structure leaderboard.
 * It return a bool variabile. True if all the operation works right, false if not.
 *
 */
bool LoadLeaderBoard();

///This function displays on screen the leaderboard
/**
 * The showLeaderboard function prints on screen all the results in the leaderboard
 * and the title of the window. It takes in input the dimension of the display.
 * @param width is the variables representing the width of the screen
 * @param height is the variables representing the height of the screen
 */
void ShowLeaderBoard(const int width, const int height, bttnbox bttn[]);


///This function takes the name of the player if he enters in the leaderboard.
/**
 * The function DrawSaveScore puts on the screen the menu section in which the 
 * player could insert is name if it has obtained a score better than the lowest 
 * score in the leaderboard.
 * It takes in input
 * @param p_name, an array of char that stores the information about the characters
 * that will be displayed in the menu about the name that the player is wrinting.
 */
void DrawSaveScore (char []);

///This function saves the leaderboard on file if it changes after a match.
/**
 * The function SaveScore takes in input a variable:
 * @param score is a integer variable that stores the result of the actual game.
 * The function controls if the score could be saved on the leaderboard, if yes it
 * takes in input the name of the player, it saves the name and the score in the
 * last position of the leaderboard and lunch the sorting function.
 * 
 * 
 */
bool SaveScore (int score);

///This is an output function to make the leaderboard comunicate with the in game function.
/**
 * The function MaxScore returns the first position in the leaderboard. This is
 * the position with the max score registered during a match and it is used in 
 * the in-game menu to display the record that has to be beat to become the
 * number one.
 */
int MaxScore();


//===========================
//  SORTING FUNCTIONS
//===========================

///Takes in in input three number and creates a minum heap.
/**
 * The MinHeapify function puts the minimum element of the sub-heap of three elements
 * that analyze in the position of the father. If function changes the element in the 
 * father position, it makes another comparison on the "old-father" new position  
 * and changes another time the father position if it is necessary. This process
 * goes on until the father isn't changed or the father becomes a "leaf" of the 
 * minHeap.
 * @param pList is a pointer to the leaderboard structure
 * @param low is the current minimum position available in the array
 * @param high is the current maximum position available in the array 
 */
void MinHeapify(player* pList, int low, int high);

/// This function create a minHeap of the the player chart.
/**
 * This function create a minHeap of the the player chart. So the element with the
 * lower value is on the first position of the array. This will be useful when the
 * BoardHeapSort sorts the leaderboard in a non-decreasing order.
 * @param pList is a pointer to the leaderboard structure
 * @param low is the current minimum position available in the array
 * @param high is the current maximum position available in the array
 */
void BuildMinHeap(player* pList, int low, int high);

///This function sorts an array.
/**
 * The function BoardHeapSort is the implementation of the HeapSort algorithm that
 * works with an array of @param leadeboard structure. It takes in input the element:
 * @param pList a pointer to the player list, the leaderboard structure.
 */
void BoardHeapSort();
#endif	/* LEADERBOARD_H */
